if AandDSystem == nil then
	AandDSystem = class({})
end

--初始抗性表
AandDSystem.DefaultResistance = 
{
	["normal".."VS".."normal"] = 1.00,
	["normal".."VS".."small"] = 1.00,
	["normal".."VS".."medium"] = 1.75,
	["normal".."VS".."large"] = 1.00,
	["normal".."VS".."fort"] = 0.75,
	["normal".."VS".."hero"] = 0.75,
	["normal".."VS".."divine"] = 0.05,
	["normal".."VS".."none"] = 1.25,

	["pierce".."VS".."normal"] = 1.00,
	["pierce".."VS".."small"] = 1.75,
	["pierce".."VS".."medium"] = 0.75,
	["pierce".."VS".."large"] = 0.75,
	["pierce".."VS".."fort"] = 0.35,
	["pierce".."VS".."hero"] = 0.50,
	["pierce".."VS".."divine"] = 0.05,
	["pierce".."VS".."none"] = 0.75,

	["magic".."VS".."normal"] = 1.00,
	["magic".."VS".."small"] = 0.75,
	["magic".."VS".."medium"] = 0.75,
	["magic".."VS".."large"] = 1.50,
	["magic".."VS".."fort"] = 0.35,
	["magic".."VS".."hero"] = 0.50,
	["magic".."VS".."divine"] = 0.05,
	["magic".."VS".."none"] = 0.75,

	["siege".."VS".."normal"] = 1.00,
	["siege".."VS".."small"] = 0.75,
	["siege".."VS".."medium"] = 0.75,
	["siege".."VS".."large"] = 1.00,
	["siege".."VS".."fort"] = 1.75,
	["siege".."VS".."hero"] = 0.50,
	["siege".."VS".."divine"] = 0.05,
	["siege".."VS".."none"] = 1.25,

	["chaos".."VS".."normal"] = 1.25,
	["chaos".."VS".."small"] = 1.25,
	["chaos".."VS".."medium"] = 1.25,
	["chaos".."VS".."large"] = 1.25,
	["chaos".."VS".."fort"] = 1.00,
	["chaos".."VS".."hero"] = 1.00,
	["chaos".."VS".."divine"] = 1.00,
	["chaos".."VS".."none"] = 1.00,
	
	["hero".."VS".."normal"] = 1.00,
	["hero".."VS".."small"] = 1.00,
	["hero".."VS".."medium"] = 1.00,
	["hero".."VS".."large"] = 1.00,
	["hero".."VS".."fort"] = 0.50,
	["hero".."VS".."hero"] = 1.00,
	["hero".."VS".."divine"] = 0.05,
	["hero".."VS".."none"] = 1.00,
}

function AandDSystem:Init()
	self.attacktype_ability =
	{
		normal="attacktype_normal",
		pierce="attacktype_pierce",
		magic="attacktype_magic",
		siege="attacktype_siege",
		chaos="attacktype_chaos",
		hero="attacktype_hero",
	}
	self.defensetype_ability =
	{
		normal="defensetype_normal",
		small="defensetype_small",
		medium="defensetype_medium",
		large="defensetype_large",
		fort="defensetype_fort",
		hero="defensetype_hero",
		divine="defensetype_divine",
		none="defensetype_none",
	}
	self.ResistanceTable = 
	{
		["normal"] = {},
		["pierce"] = {},
		["magic"] = {},
		["siege"] = {},
		["chaos"] = {},
		["hero"] = {},
	}
	self.Resistance = 
	{
		["normal"] = 1,
		["pierce"] = 1,
		["magic"] = 1,
		["siege"] = 1,
		["chaos"] = 1,
		["hero"] = 1,
	}
end
function GetAandDSystem(hunit)
	return hunit.AD
end
function AandDSystem:UpdateResistance(sattacktype)
	local r = 1
	for i,resistance in pairs(self.ResistanceTable[sattacktype]) do
		r = r * (1-r)
	end
	self.Resistance[sattacktype] = 1-r
end
--sname为索引，可以用做修改或删除所添加的抗性，不填，则返回一个系统随机生成的索引
function AandDSystem:AddAttacktypeResistance(sattacktype,fresistance,sname)
	print("AttackType:"..sattacktype)
	print("Resistance:"..fresistance)
	print("Name:"..sname)
	if not (sattacktype == "normal" or sattacktype == "pierce" or sattacktype == "magic" or sattacktype == "siege" or sattacktype == "chaos" or sattacktype == "hero") then
		return
	end
	if not sname then
		sname = DoUniqueString(sattacktype)
	end
	AandDSystem.ResistanceTable[sattacktype][sname] = fresistance
	AandDSystem:UpdateResistance(sattacktype)
	return sname
end
function AandDSystem:RemoveAttacktypeResistance(sattacktype,sname)
	if not (sattacktype == "normal" or sattacktype == "pierce" or sattacktype == "magic" or sattacktype == "siege" or sattacktype == "chaos" or sattacktype == "hero") then
		return
	end
	if type(sname) ~= "string" then
		return
	end
	self.ResistanceTable[sattacktype][sname] = 0
	self:UpdateResistance(sattacktype)
end
function AandDSystem:GetAttacktypeResistance(sattacktype)
	return self.Resistance[sattacktype]
end
function AandDSystem:GetUnit()
	return self.Unit
end
function AandDSystem:GetAttackType()
	return self.AttackType
end
function AandDSystem:GetDefensetype()
	return self.Defensetype
end
--给单位添加攻防系统，并且初始化抗性
function CreateAandDSystem(hunit,sattacktype,sdefensetype)
	local AandD = AandDSystem
	AandD:Init()
	AandD.Unit = hunit
	hunit.AD = AandD

	AandD.AttackType = sattacktype
	AandD.Unit:AddAbility(AandD.attacktype_ability[sattacktype])
	AandD.Attacktype_Abiltiy = AandD.Unit:FindAbilityByName(AandD.attacktype_ability[sattacktype])
	AandD.Attacktype_Abiltiy:SetLevel(1)

	AandD.Defensetype = sdefensetype
	AandD.Unit:AddAbility(AandD.defensetype_ability[sdefensetype])
	AandD.Defensetype_Abiltiy = AandD.Unit:FindAbilityByName(AandD.defensetype_ability[sdefensetype])
	AandD.Defensetype_Abiltiy:SetLevel(1)

	--初始化抗性
	for i,v in pairs(AandD.attacktype_ability) do
		AandD.AddAttacktypeResistance(nil, i , AandD.DefaultResistance[i.."VS"..sdefensetype] , v )
		--print(AandD:GetAttacktypeResistance(i))
	end

	return AandD
end